package com.hdculture.simple.expensemanager.premium;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class CoreDBHelper extends SQLiteOpenHelper {
	private static final String DATABASE_NAME = "applicationdata";
	public static final String SETTINGS_FIELDNAME="FieldName";
	public static final String SETTINGS_FIELDVALUE="FieldValue";
	private static final String SETTINGS_DATABASE_TABLE = "settings";
	
	private static final int DATABASE_VERSION = 1;

	// Database creation sql statement
	private static final String INCOME_DATABASE_CREATE = "create table incomeData (_id integer primary key autoincrement, "
			+ "dt integer not null, month integer not null, year integer not null, source text not null, amount integer not null);";
	private static final String EXPENSE_DATABASE_CREATE = "create table expenseData (_id integer primary key autoincrement, "
		+ "dt integer not null, month integer not null, year integer not null, description text not null, amount integer not null);";
	private static final String SETTINGS_DATABASE_CREATE = "create table settings (FieldName text not null, FieldValue text not null);";
	public CoreDBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	// Method is called during creation of the database
	@Override
	public void onCreate(SQLiteDatabase database) {
		database.execSQL(INCOME_DATABASE_CREATE);
		database.execSQL(EXPENSE_DATABASE_CREATE);
		database.execSQL(SETTINGS_DATABASE_CREATE);
		ContentValues cv = new ContentValues();
		cv.put(SETTINGS_FIELDNAME, "PASSWORD");
		cv.put(SETTINGS_FIELDVALUE, "");
		database.insert(SETTINGS_DATABASE_TABLE, null, cv);
		cv = new ContentValues();
		cv.put(SETTINGS_FIELDNAME, "CURRENCYSIGN");
		cv.put(SETTINGS_FIELDVALUE, "$");
		database.insert(SETTINGS_DATABASE_TABLE, null, cv);
	}

	// Method is called during an upgrade of the database, e.g. if you increase
	// the database version
	@Override
	public void onUpgrade(SQLiteDatabase database, int oldVersion,
			int newVersion) {
		Log.w(CoreDBHelper.class.getName(),
				"Upgrading database from version " + oldVersion + " to "
						+ newVersion + ", which will destroy all old data");
		database.execSQL("DROP TABLE IF EXISTS incomeData");
		database.execSQL("DROP TABLE IF EXISTS expenseData");
		onCreate(database);
	}
}
